package com.rainng.coursesystem.repository;

import com.rainng.coursesystem.model.entity.CourseEntity;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class StudentRepository {

    private final JdbcTemplate jdbcTemplate;

    public StudentRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public String findUserNameById(long userId) {
        String sql = "SELECT student_name FROM rc_student WHERE student_id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{userId}, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                return rs.getString("student_name");
            }
        });
    }

    public List<Long> findStudentByCourseId(long courseId) {
        String sql = "SELECT sc_student_id FROM rc_student_course WHERE sc_course_id = ?";
        return jdbcTemplate.query(sql, new Object[]{courseId}, new RowMapper<Long>() {
            @Override
            public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
                return rs.getLong("sc_student_id");
            }
        });
    }

    public String findCourseNameById(Long courseId) {
        String sql = "SELECT course_name FROM rc_course WHERE course_id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{courseId}, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                return rs.getString("course_name");
            }
        });
    }
}
